package tania.coca.ejercicios.tarjetas;

import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import java.awt.Font;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.BorderFactory;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;

import java.awt.GridBagConstraints;
import java.awt.SystemColor;
import java.util.ArrayList;
import java.util.Random;

public class Llamadas extends JFrame {

	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;

	private JPanel jPanel = null;

	private JLabel jLabel = null;

	private JPanel jPanel1 = null;

	private JLabel jLabel1 = null;

	private JTextField jTextField = null;

	private JScrollPane jScrollPane = null;

	private JTable jTable = null;

	private JButton jButton = null;

	private JPanel jPanel2 = null;

	private JLabel jLabel2 = null;

	private JLabel jLabel3 = null;

	private JPanel jPanel3 = null;

	private JLabel jLabel4 = null;
	
	private JOptionPane pane = new JOptionPane();

	private JTextField jTextField1 = null;

	private JButton jButton1 = null;

	private JButton jButton2 = null;

	private JLabel jLabel5 = null;

	private JLabel jLabel6 = null;
	
	private DefaultTableModel model = new DefaultTableModel();

	private ArrayList<Tarjeta> tarjetas = new ArrayList<Tarjeta>();
	
	private Thread llamar = null;


	/**
	 * This is the default constructor
	 */
	public Llamadas() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(619, 370);
		this.setContentPane(getJContentPane());
		this.setTitle("Llamadas");
		addColumns();
		jTable.setModel(model);
		jButton2.setEnabled(false);
		jButton1.setEnabled(false);
		
	}

	
	private void addColumns(){
		
		model.addColumn("Numero Tarjeta");
		model.addColumn("Minutos");
		model.addColumn("Segundos");
		
	}
	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getJPanel(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jLabel = new JLabel();
			jLabel.setHorizontalAlignment(SwingConstants.CENTER);
			jLabel.setBounds(new Rectangle(59, 12, 135, 15));
			jLabel.setText("Generar Tarjeta");
			jPanel = new JPanel();
			jPanel.setLayout(null);
			jPanel.setBounds(new Rectangle(5, 4, 622, 333));
			jPanel.add(getJPanel1(), null);
			jPanel.add(getJPanel2(), null);
		}
		return jPanel;
	}

	/**
	 * This method initializes jPanel1	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			jLabel1 = new JLabel();
			jLabel1.setBounds(new Rectangle(19, 45, 84, 15));
			jLabel1.setText("Monto RD$");
			jPanel1 = new JPanel();
			jPanel1.setLayout(null);
			jPanel1.setBounds(new Rectangle(10, 10, 263, 313));
			jPanel1.setBorder(BorderFactory.createLineBorder(Color.gray, 2));
			jPanel1.add(jLabel, null);
			jPanel1.add(jLabel1, null);
			jPanel1.add(getJTextField(), null);
			jPanel1.add(getJScrollPane(), null);
			jPanel1.add(getJButton(), null);
		}
		return jPanel1;
	}

	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField() {
		if (jTextField == null) {
			jTextField = new JTextField();
			jTextField.setBounds(new Rectangle(109, 39, 115, 28));
			jTextField.setFont(new Font("Electron", Font.BOLD, 12));
		}
		return jTextField;
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setBounds(new Rectangle(8, 140, 245, 165));
			jScrollPane.setViewportView(getJTable());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes jTable	
	 * 	
	 * @return javax.swing.JTable	
	 */
	private JTable getJTable() {
		if (jTable == null) {
			jTable = new JTable();
		}
		return jTable;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setBounds(new Rectangle(15, 86, 230, 42));
			jButton.setIcon(new ImageIcon(getClass().getResource("/tania/coca/ejercicios/tarjetas/credit-cards-32x32.png")));
			jButton.setText("Generar Tarjeta");
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
				
					try{
					
					int monto = Integer.parseInt((jTextField.getText()));
					int minutos = Tarjeta.getMinutosFromMonto(monto);
					
					if(minutos != 0){
						
						Object[] row = {Tarjeta.generarNumeroTarjeta(),minutos,00};
						model.addRow(row);						
						jTextField.setText("");
						jTextField.setFocusable(true);
						
					}else{						
						
						pane.showMessageDialog(null, "Monto invalido. \nIntentelo con RD$60.00,RD$100.00,RD$200.00 o RD$500.00", "Monto Incorrecto", JOptionPane.ERROR_MESSAGE);
						jTextField.setText("");
						jTextField.setFocusable(true);
						
					}
					
					}catch(NumberFormatException nfe){
						
						pane.showMessageDialog(null, "No se puede generar un numero de tarjeta si el campo esta vacio. \nIngrese un valor valido", "Monto Incorrecto", JOptionPane.ERROR_MESSAGE);
						jTextField.setText("");
						jTextField.setFocusable(true);
						
						
					}
					
				}
			});
		}
		return jButton;
	}

	/**
	 * This method initializes jPanel2	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel2() {
		if (jPanel2 == null) {
			jLabel6 = new JLabel();
			jLabel6.setBounds(new Rectangle(14, 77, 133, 108));
			jLabel6.setHorizontalAlignment(SwingConstants.CENTER);
			jLabel6.setFont(new Font("Dialog", Font.BOLD, 42));
			jLabel6.setForeground(Color.green);
			jLabel6.setText("00:00");
			jLabel6.addPropertyChangeListener("text",
					new java.beans.PropertyChangeListener() {
						public void propertyChange(java.beans.PropertyChangeEvent e) {
							
							if(jLabel6.getText().equals("00:00")){
								
								jTextField1.setText("");
								jButton2.setEnabled(false);
								
							}
						
						}
					});
			jLabel5 = new JLabel();
			jLabel5.setBounds(new Rectangle(9, 49, 83, 15));
			jLabel5.setText("Minutos:");
			jLabel4 = new JLabel();
			jLabel4.setBounds(new Rectangle(9, 197, 118, 15));
			jLabel4.setText("Numero Tarjeta");
			jLabel3 = new JLabel();
			jLabel3.setIcon(new ImageIcon(getClass().getResource("/tania/coca/ejercicios/tarjetas/Phone-w128x128.png")));
			jLabel3.setText("");
			jLabel2 = new JLabel();
			jLabel2.setBounds(new Rectangle(7, 6, 142, 38));
			jLabel2.setHorizontalAlignment(SwingConstants.CENTER);
			jLabel2.setFont(new Font("Khalid", Font.BOLD, 24));
			jLabel2.setForeground(Color.blue);
			jLabel2.setText("Telefono");
			jPanel2 = new JPanel();
			jPanel2.setLayout(null);
			jPanel2.setBounds(new Rectangle(279, 9, 325, 314));
			jPanel2.setBorder(BorderFactory.createLineBorder(SystemColor.activeCaption, 2));
			jPanel2.add(jLabel2, null);
			jPanel2.add(getJPanel3(), null);
			jPanel2.add(jLabel4, null);
			jPanel2.add(getJTextField1(), null);
			jPanel2.add(getJButton1(), null);
			jPanel2.add(getJButton2(), null);
			jPanel2.add(jLabel5, null);
			jPanel2.add(jLabel6, null);
		}
		return jPanel2;
	}

	/**
	 * This method initializes jPanel3	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel3() {
		if (jPanel3 == null) {
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.gridy = 0;
			jPanel3 = new JPanel();
			jPanel3.setLayout(new GridBagLayout());
			jPanel3.setBounds(new Rectangle(154, 8, 161, 176));
			jPanel3.setBorder(BorderFactory.createTitledBorder(null, "Estado: Cerrado", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
			jPanel3.add(jLabel3, gridBagConstraints);
		}
		return jPanel3;
	}

	/**
	 * This method initializes jTextField1	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField1() {
		if (jTextField1 == null) {
			jTextField1 = new JTextField();
			jTextField1.setBounds(new Rectangle(11, 217, 294, 28));
			jTextField1.setFont(new Font("Dialog", Font.BOLD, 18));
			jTextField1.setForeground(Color.green);
			jTextField1.setHorizontalAlignment(JTextField.CENTER);
			jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
				public void keyTyped(java.awt.event.KeyEvent e) {
					
					if((jTextField1.getText().length() >=18)&&(!jButton2.isEnabled())){
						
						jButton1.setEnabled(true);
						
					}else{
						
						jButton1.setEnabled(false);
					}
				
				}
			});
		}
		return jTextField1;
	}

	/**
	 * This method initializes jButton1	
	 * 	
	 * @return javax.swing.JButton	
	 */
	
	private int tarjetaValida(String numeroTarjeta){
		
		int index = -1;
		
		for (int i = 0; i < model.getRowCount(); i++) {
			
			if(numeroTarjeta.equals(model.getValueAt(i, 0))){
				
				index =  i;
			}
			
			
		}
		
		return index;
		
		
	}
	
	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setBounds(new Rectangle(12, 254, 139, 37));
			jButton1.setIcon(new ImageIcon(getClass().getResource("/tania/coca/ejercicios/tarjetas/Text-Bubble-24x24.png")));
			jButton1.setText("Llamar");
			jButton1.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
										
					
					
					String numeroTarjeta = jTextField1.getText();
					int index = tarjetaValida(numeroTarjeta);
										
					if(index != -1){
						
					jPanel3.setBorder(BorderFactory.createTitledBorder(null, "Estado: Hablando", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
					jLabel3.setIcon(new ImageIcon(getClass().getResource("/tania/coca/ejercicios/tarjetas/Chat-128x128.png")));
					Tarjeta tarjeta = new Tarjeta(model.getValueAt(index, 0).toString(),(Integer)model.getValueAt(index, 1),(Integer)model.getValueAt(index, 2));
					
					llamar = new Thread(new Llamar(tarjeta,jLabel6,model,index));
					llamar.start();					
				

					jButton2.setEnabled(true);
					jButton1.setEnabled(false);
					
					}else{
						
						pane.showMessageDialog(null, "El numero de tarjeta es invalido","Tarjeta Invalida",JOptionPane.ERROR_MESSAGE);
						jTextField1.setText("");
					}
					
				}
			});
		}
		return jButton1;
	}

	/**
	 * This method initializes jButton2	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton2() {
		if (jButton2 == null) {
			jButton2 = new JButton();
			jButton2.setBounds(new Rectangle(158, 255, 145, 35));
			jButton2.setIcon(new ImageIcon(getClass().getResource("/tania/coca/ejercicios/tarjetas/Delete-24x24.png")));
			jButton2.setText("Colgar");
			jButton2.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					
					jLabel3.setIcon(new ImageIcon(getClass().getResource("/tania/coca/ejercicios/tarjetas/Phone-w128x128.png")));
					jPanel3.setBorder(BorderFactory.createTitledBorder(null, "Estado: Cerrado", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
					llamar.stop();
					jLabel6.setText("00:00");
					jTextField1.setText("");
					jButton2.setEnabled(false);
					jButton1.setEnabled(true);
					
					
				}
			});
		}
		return jButton2;
	}

	public static void main(String[] args) {
		
		Llamadas llamadas = new Llamadas();
		llamadas.setVisible(true);
		llamadas.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
	}
	
}  //  @jve:decl-index=0:visual-constraint="24,10"
